var selectedRow;
$(function () {
    if(oneBasicDoctorInfo && oneBasicDoctorInfo.certificateMedia){
        console.log(oneBasicDoctorInfo.certificateMedia);
        var certificateMedias = oneBasicDoctorInfo.certificateMedia.split(',');
        if(certificateMedias[0]){
            $("#coverUrlImg2").attr("src", certificateMedias[0]);
        }
        if(certificateMedias[1]){
            $("#coverUrlImg3").attr("src", certificateMedias[1]);
        }
        if(certificateMedias[2]){
            $("#coverUrlImg4").attr("src", certificateMedias[2]);
        }
    }
});

// dataTable
$('#dataTable').on("click-row.bs.table", function (e, row, $element) {
    // checkBox 的勾选
    var index = $element[0].rowIndex - 1;
    $('#dataTable').bootstrapTable('check', index);
    $('#dataTable .success').removeClass('success');//去除之前选中的行的，选中样式
    $element.addClass('success');//添加当前选中的 success样式用于区别
    selectedRow = row;
    doRefreshLog();
});

function checkPass() {
    swalToastr('审核通过',  '确定通过审核吗？', 'warning', doCheckMergeData, '审核通过');
}

function doCheckMergeData() {
    if(!oneBasicDoctorInfo || !oneBasicDoctorInfo.docCode){
        return;
    }
    if(oneBasicDoctorInfo.docMergeCode){
        //存在合并记录，暂不处理
        doPassHasMergeed();
        return;
    }
    var postData = {
        docCode : oneBasicDoctorInfo.docCode
    };
    $.ajax({
        type: 'post',
        url: basePath + 'checkDoc/checkMergeDocs?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error('提交失败');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            if(data.isSuccess && data.displayMsg && data.displayMsg == 'Y'){
                //需要合并
                $("#mergeModal").modal('show');
                $('#dataTable').bootstrapTable('refresh', {
                    url: basePath + 'checkDoc/pageFindMergeDocData?t=' + new Date().getTime(),
                    query: {
                        docCode : oneBasicDoctorInfo.docCode
                    },
                });
            } else if(data.isSuccess && data.displayMsg && data.displayMsg == 'N'){
                //不需要合并
                doPassNoMerge();
            } else {
                //错误
            }
        },
    });
}

/*通过-不合并*/
function doPassNoMerge() {
    var postData = {
        checkId : $("#checkId").val(),
        id : oneBasicDoctorInfo.id,
        docCode : oneBasicDoctorInfo.docCode
    };
    $.ajax({
        type: 'post',
        url: basePath + 'checkDoc/doPassNoMerge?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error('合并失败');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            if(data.isSuccess){
                doBack();
            }
        },
    });
}

/*通过-合并*/
function doPassMerge() {
    var mergeDoc = getSelectedRows('#dataTable');
    if(!mergeDoc || mergeDoc.length < 1){
        toastr.error('请选择需要合并的专家！');
        return;
    }
    var postData = {
        checkId : $("#checkId").val(),
        docCode : oneBasicDoctorInfo.docCode,
        id : oneBasicDoctorInfo.id,
        docMergeCode : mergeDoc[0].docCode
    };
    $.ajax({
        type: 'post',
        url: basePath + 'checkDoc/doPassMerge?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error('合并失败');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            if(data.isSuccess){
                doBack();
            }
        },
    });
}

/*通过-已经合并过的数据*/
function doPassHasMergeed() {
    var postData = {
        checkId : $("#checkId").val(),
        docCode : oneBasicDoctorInfo.docCode,
        id : oneBasicDoctorInfo.id,
        docMergeCode : oneBasicDoctorInfo.docMergeCode
    };
    $.ajax({
        type: 'post',
        url: basePath + 'checkDoc/doPassHasMergeed?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error('审核已合并数据失败');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            if(data.isSuccess){
                doBack();
            }
        },
    });
}

function openDoUnPassPage() {
    $('#unPassModal').modal('show');
}

/*审核不通过*/
function doUnPass() {
    var reason = $('#reason').val();
    if (!reason) {
        toastr.error('原因不能为空');
        return;
    }
    var postData = {
        checkId : $("#checkId").val(),
        docCode : oneBasicDoctorInfo.docCode,
        reason : reason,
        id : oneBasicDoctorInfo.id,
        docMergeCode : oneBasicDoctorInfo.docMergeCode
    };
    $.ajax({
        type: 'post',
        url: basePath + 'checkDoc/doUnPass?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error('不通过操作失败');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            if(data.isSuccess){
                doBack();
            }
        },
    });
}

/*关闭合并层*/
function closeMergeModal() {
    $("#mergeModal").modal('hide');
}

function closeUnPassModal() {
    $('#reason').val('');
    $("#unPassModal").modal('hide');
}

$('#unPassModal').on('hidden.bs.modal', function () {
    // 执行一些动作...
    $('#reason').val('');
});

/*返回*/
function doBack() {
    if(page && page == '1'){
        var str = '&docName=' + searchInfo.docName + '&isvPid=' + searchInfo.isvPid + '&isvName=' + searchInfo.isvName
            + '&checkType=' + searchInfo.checkType + '&timeInterval=' + searchInfo.timeInterval + '&orgCode=' + searchInfo.orgCode
            + '&isCheckExist=' + searchInfo.isCheckExist + '&isQueryExist=' + searchInfo.isQueryExist
            + '&orgName=' + searchInfo.orgName + '&pageNo=' + searchInfo.pageNo + '&pageSize=' + searchInfo.pageSize;
        window.location.href = basePath + 'checkDoc/pendingCheckDocPage?t=' + new Date().getTime() + str;
    } else {
        var str = '&docName=' + searchInfo.docName + '&isvPid=' + searchInfo.isvPid + '&isvName=' + searchInfo.isvName + '&result=' + searchInfo.result
            + '&checkType=' + searchInfo.checkType + '&timeInterval=' + searchInfo.timeInterval + '&orgCode=' + searchInfo.orgCode
            + '&isCheckExist=' + searchInfo.isCheckExist + '&isQueryExist=' + searchInfo.isQueryExist
            + '&orgName=' + searchInfo.orgName + '&pageNo=' + searchInfo.pageNo + '&pageSize=' + searchInfo.pageSize;
        window.location.href = basePath + 'checkDoc/checkedDocPage?t=' + new Date().getTime() + str;
    }
}